package com.ruoyi.web.controller.farmer;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.farmer.domain.Species;
import com.ruoyi.farmer.service.ISpeciesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 农产品种类Controller
 *
 * @author 姚毫征
 * @date 2023-09-14
 */
@RestController
@RequestMapping("/species/species")
public class SpeciesController extends BaseController
{
    @Autowired
    private ISpeciesService speciesService;

    /**
     * 查询农产品种类列表
     */
    @PreAuthorize("@ss.hasPermi('species:species:list')")
    @GetMapping("/list")
    public TableDataInfo list(Species species)
    {
        startPage();
        List<Species> list = speciesService.selectSpeciesList(species);
        return getDataTable(list);
    }

    /**
     * 导出农产品种类列表
     */
    @PreAuthorize("@ss.hasPermi('species:species:export')")
    @Log(title = "农产品种类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Species species)
    {
        List<Species> list = speciesService.selectSpeciesList(species);
        ExcelUtil<Species> util = new ExcelUtil<Species>(Species.class);
        util.exportExcel(response, list, "农产品种类数据");
    }

    /**
     * 获取农产品种类详细信息
     */
    @PreAuthorize("@ss.hasPermi('species:species:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(speciesService.selectSpeciesById(id));
    }
    /**
     * 获取农产品种类详细信息
     */
    @PreAuthorize("@ss.hasPermi('species:species:getAllSpecies')")
    @GetMapping(value = "/getAllSpecies")
    public AjaxResult getAllSpecies()
    {

        return success(speciesService.getAllSpecies());
    }

    /**
     * 新增农产品种类
     */
    @PreAuthorize("@ss.hasPermi('species:species:add')")
    @Log(title = "农产品种类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Species species)
    {
        species.setSpeciesId(UUID.randomUUID().toString());
        species.setCreateTime(DateUtils.getTime());
        return toAjax(speciesService.insertSpecies(species));
    }

    /**
     * 修改农产品种类
     */
    @PreAuthorize("@ss.hasPermi('species:species:edit')")
    @Log(title = "农产品种类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Species species)
    {
        species.setUpdateTime(DateUtils.getTime());
        return toAjax(speciesService.updateSpecies(species));
    }

    /**
     * 删除农产品种类
     */
    @PreAuthorize("@ss.hasPermi('species:species:remove')")
    @Log(title = "农产品种类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(speciesService.deleteSpeciesByIds(ids));
    }
}
